package cn.hnu.qq.global;

import java.io.IOException;
import java.net.ServerSocket;

import javax.swing.JProgressBar;

public class FileReceiver extends Thread {
	private ServerSocket s = null;
	private FileReceiverThread r;
	private JProgressBar progress;

	private String filepath = "";
	
	public FileReceiver() {
	}
	public FileReceiver(String filepath) {
		this.filepath = filepath;
	}
	public boolean bind(int port){
		try {
			s = new ServerSocket(port);
		} catch (IOException e) {
			return false;
		}
		System.out.println("接收端建立" + s);
		return true;
	}
	public void run() {
		try {
			java.net.Socket socket = s.accept();
			System.out.println("发送端连接建立" + socket);
			r = new FileReceiverThread(socket,filepath);
			r.setProgress(progress);
			r.start();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (java.lang.Exception ex) {
			System.err.println(ex.getMessage().toString());
			ex.printStackTrace();
		} finally {
			try {
				s.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public boolean isSendOver(){
		if(r == null){
			return false;
		}
		return r.isSendOver();
	}
	public void setProgress(JProgressBar progress){
		this.progress = progress;
	}
}
